<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Shipment
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for shipment
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Shipment
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_Shipment extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'shipment';

    /**
     * Save shipment
     * @param array $row
     * @return integer
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $row['created'] = new Zend_Db_Expr('NOW()');
            $this->insert($row);
        }
    }

    /**
     * Get shipments by filter 
     * @return array 
     */
    public function getAllByFilter($id, $toName, $quantityFrom, $quantityTo, $costFrom, $costTo, $updatedFrom, $updatedTo, $createdFrom, $createdTo, $orderId, $shipmentStatusId, $sort, $direction, $page, $rowCount)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('s' => $this->_name), array('s.id', 's.to_name', 's.quantity', 's.cost', 's.updated', 's.created', 's.order_id'));
        $query->join(array('ss' => 'shipment_status'), 's.shipment_status_id = ss.id', 'ss.name as shipment_status_name');

        // Check filters
        if ($id) {
            $query->where('s.id = ?', $id);
        }
        if ($toName) {
            $query->where('s.to_name LIKE ?', $toName . '%');
        }
        if ($quantityFrom) {
            $query->where('s.quantity >= ?', $quantityFrom);
        }
        if ($quantityTo) {
            $query->where('s.quantity <= ?', $quantityTo);
        }
        if ($costFrom) {
            $query->where('s.cost >= ?', $costFrom);
        }
        if ($costTo) {
            $query->where('s.cost <= ?', $costTo);
        }
        if ($updatedFrom) {
            $query->where('s.updated >= ?', $updatedFrom);
        }
        if ($updatedTo) {
            $query->where('s.updated <= ?', $updatedTo);
        }
        if ($createdFrom) {
            $query->where('s.created >= ?', $createdFrom);
        }
        if ($createdTo) {
            $query->where('s.created <= ?', $createdTo);
        }
        if ($orderId) {
            $query->where('s.order_id = ?', $orderId);
        }
        if ($shipmentStatusId) {
            $query->where('s.shipment_status_id = ?', $shipmentStatusId);
        }
        if ($sort && $direction) {
            $query->order($sort . ' ' . $direction);
        }

        $query->limitPage($page, $rowCount);

        // Get shipments from DB
        $result = $this->getAdapter()->fetchAll($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

    /**
     * Get count of shipments by filter 
     * @return integer 
     */
    public function getCountByFilter($id, $toName, $quantityFrom, $quantityTo, $costFrom, $costTo, $updatedFrom, $updatedTo, $createdFrom, $createdTo, $orderId, $shipmentStatusId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('s' => $this->_name), new Zend_Db_Expr('COUNT(*)'));

        // Check filters
        if ($id) {
            $query->where('s.id = ?', $id);
        }
        if ($toName) {
            $query->where('s.to_name LIKE ?', $toName . '%');
        }
        if ($quantityFrom) {
            $query->where('s.quantity >= ?', $quantityFrom);
        }
        if ($quantityTo) {
            $query->where('s.quantity <= ?', $quantityTo);
        }
        if ($costFrom) {
            $query->where('s.cost >= ?', $costFrom);
        }
        if ($costTo) {
            $query->where('s.cost <= ?', $costTo);
        }
        if ($updatedFrom) {
            $query->where('s.updated >= ?', $updatedFrom);
        }
        if ($updatedTo) {
            $query->where('s.updated <= ?', $updatedTo);
        }
        if ($createdFrom) {
            $query->where('s.created >= ?', $createdFrom);
        }
        if ($createdTo) {
            $query->where('s.created <= ?', $createdTo);
        }
        if ($orderId) {
            $query->where('s.order_id = ?', $orderId);
        }
        if ($shipmentStatusId) {
            $query->where('s.shipment_status_id = ?', $shipmentStatusId);
        }

        // Get count of shipments from DB
        $result = $this->getAdapter()->fetchOne($query);

        return $result;
    }

    /**
     * Get shipment by order ID
     * @param integer $orderId
     * @return array 
     */
    public function getByOrderId($orderId)
    {
        $query = $this->getAdapter()->select();
        $query->from(array('s' => $this->_name), array('s.id', 's.to_name', 's.quantity', 's.cost', 's.updated', 's.created', 's.shipment_status_id'));
        $query->join(array('ss' => 'shipment_status'), 's.shipment_status_id = ss.id', 'ss.name as shipment_status_name');
        $query->where('s.order_id = ?', $orderId);

        // Get order from DB
        $result = $this->getAdapter()->fetchRow($query, null, Zend_Db::FETCH_OBJ);
        return $result;
    }

}
